package com.example.springmvc.common;

import com.example.springmvc.config.SessionUser;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

/**
 * @author fanhang
 */
@Deprecated
public abstract class RequestHolder {

    public static void setRequestScope(String name, Object value) {
        RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();
        attributes.setAttribute(name, value, RequestAttributes.SCOPE_REQUEST);
    }

    public static Long getSessionUser() {
        return (Long) getRequestScope(SessionUser.SESSION_ATTR_NAME);
    }

    public static void setSessionUser(Long userId) {
        setRequestScope(SessionUser.SESSION_ATTR_NAME, userId);
    }

    public static Object getRequestScope(String name) {
        RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();
        return attributes.getAttribute(name, RequestAttributes.SCOPE_REQUEST);
    }
}
